package com.myself.model.demo.netty.foundation;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
/**
 * @author Czx
 */
public class MyClientBootStrap {


    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup eventExecutors = new NioEventLoopGroup(1);
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(eventExecutors)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast("messageDecoder", new NettyDecoder());
                        socketChannel.pipeline().addLast("messageEncoder", new NettyEncoder());
                        socketChannel.pipeline().addLast(new NettyClientHandler());
                    }
                });
        ChannelFuture sync = bootstrap.connect("127.0.0.1", 9999).sync();
        sync.channel().closeFuture().sync();
        eventExecutors.shutdownGracefully();

    }
}
